//
//  MTCacheManager.swift
//  MT
//
//  Created by homing tsui on 2018/12/8.
//  Copyright © 2019年 张强. All rights reserved.
//

import UIKit

public class MTCacheManager: NSObject {
    static public let `default`: MTCacheManager = MTCacheManager()
}

// MARK: 关于计算文件夹大小 extensions
extension MTCacheManager {
    
    /*
     @method        fileSizeAtPath(filePath: String)
     @abstract      计算单个文件夹大小
     @param         filePath: 文件夹路径
     @discussion    计算单个文件夹大小并返回文件夹大小
     */
    fileprivate func fileSizeAtPath(filePath: String) -> Double {
        let manager = FileManager.default
        var fileSize: Double = 0.0
        do {
            let attr: NSDictionary = try manager.attributesOfItem(atPath: filePath) as NSDictionary
            fileSize = Double(attr.fileSize())
        } catch {
            debugPrint(error.localizedDescription)
        }
        return fileSize/1024/1024
    }
    
    /*
     @method        cachesFolderSize()
     @abstract      caches文件夹文件总大小
     */
    fileprivate func cachesFolderSize() -> Double {
        let folderPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first
        let subPaths = try? FileManager.default.contentsOfDirectory(atPath: folderPath!)
        let manager = FileManager.default
        
        if !manager.fileExists(atPath: folderPath!) { return 0 }
        var fileSize: Double = 0.0
        if let subPaths = subPaths {
            for path in subPaths {
                fileSize = fileSize + fileSizeAtPath(filePath: folderPath! + "/" + path)
            }
        } else {
            return 0
        }
        return fileSize
    }
    
    /*
     @method        folderSize()
     @abstract      缓存文件夹总大小
     */
    public func folderSize() -> String {
        return String.init(format: "%.1f", MTCacheManager.default.cachesFolderSize())
    }
}

// MARK: 关于清除文件夹缓存 extensions
extension MTCacheManager {
    
    /*
     @method        clearAllCache(withSuccessClosure completeHandler:)
     @abstract      清除所有缓存
     @param         completeHandler: 完成闭包, 清除所有缓存后的闭包
     */
    public func clearAllCache(withSuccessClosure completeHandler: (()->())?) {
        DispatchQueue.global().async {
            
            let directoryPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first
            let subPaths = try? FileManager.default.contentsOfDirectory(atPath: directoryPath!)
            
            if let subPaths = subPaths {
                for subPath in subPaths {
                    let filePath = directoryPath! + "/" + subPath
                    
                    if filePath.contains("Snapshots") { continue }
                    debugPrint(filePath)
                    do {
                        try FileManager.default.removeItem(at: URL(fileURLWithPath: filePath))
                    } catch {
                        debugPrint(error.localizedDescription)
                    }
                }
                
                DispatchQueue.main.async {
                    if let handler = completeHandler {
                        handler()
                    }
                }
            }
        }
    }
    
}
